<?php
// +----------------------------------------------------------------------
// | NewThink [ Think More,Think Better! ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2018 http://www.newthink.cc All rights reserved.
// +----------------------------------------------------------------------
// | 版权所有：山西创泰网络科技有限公司
// +----------------------------------------------------------------------
// | Author: JiYun Date:2019-03-04 Time:14:24
// +----------------------------------------------------------------------

namespace app\common\service\airquality;

use app\common\service\BaseService;
use app\common\service\airquality\AirQualityStationAqiForecastDayService;
use app\common\service\airquality\AirQualityStationAqiRealtimeDayService;
use think\Db;

/**
 * Class AirQualityService
 * @package app\index\service\airquality
 */
class AirQualityService extends BaseService
{
    /**
     * AirQualityService constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getAirQuality7DaysDemo($startTime, $endTime)
    {
        $sql = 'select * from air_quality_pollutant_company_forecast where predict_time >= ? AND predict_time <= ?';
        Db::query($sql, [$startTime, $endTime]);
        $result = Db::query($sql);
        return $result;
    }
    
    /**
     * 判断参数
     * @author JiYun On 2019/03/04
     * @param array $param
     * @return array
     */
    private function checkParam($param = [])
    {
        // 设置默认值
        if (empty($param['station'])) {     // 站点
            $param['station'] = '4';
        }
        
        if (empty($param['city'])) {        // 城市
            $param['city'] = '53';
        }
        
        if (empty($param['start_time'])) {   // 开始时间 和 结束时间
            $param['start_time'] = date('Y-m-d', time()) . ' 00:00:00';
            $param['end_time']   = date('Y-m-d', strtotime('+6 day')) . ' 23:59:59';
        }
        
        if (empty($param['end_time'])) {     // 结束时间
            $param['end_time'] = date('Y-m-d', strtotime($param['start_time']) + 60 * 60 * 24 * 6) . ' 23:59:59';
        }
        
        if (empty($param['publish_time'])) { // 发布时间
            /*
             * 因为预报时间有多个，因此必须增加发布时间
             */
            $param['publish_time'] = date('Y-m-d', time()) . ' 00:00:00';
        }
        
        if (empty($param['pollutant'])) {    // 污染物
            $param['pollutant'] = 'pm25';
        }
        
        return $param;
    }
    
    /**
     * 获取7天的数据
     * @author JiYun On 2019/03/04
     * @param array $param
     * @return array
     * @throws \Exception
     */
    private function get7DayDatas($param = [])
    {
        $list = [];
        
        $stationForecastDayService = new AirQualityStationAqiForecastDayService();
        $stationRealtimeDayService = new AirQualityStationAqiRealtimeDayService();
        
        try {
            $forecastList = $stationForecastDayService->get7DayForecastList($param);
            $realtimeList = $stationRealtimeDayService->get7DayRealtimeList($param);
            
            $list['forecast'] = $forecastList;
            $list['real']     = $realtimeList;
        } catch (\Exception $e) {
            throw $e;
        }
        
        return $list;
    }
    
    /**
     * 获取7天的空气质量趋势图
     * @author JiYun On 2019/03/04
     * @param array $param
     * @return mixed
     * @throws \Exception
     */
    public function getAirQualityTrend($param = [])
    {
        // 设置默认值
        $param = $this->checkParam($param);
        
        $list = [];
        
        try {
            // 取数据
            $temp = $this->get7DayDatas($param);
            
            $days     = [];
            $real     = [];
            $forecast = [];
            
            // 合并列表
            for ($i = 0; $i < count($temp['forecast']); ++$i) {
                $days[]     = mb_substr($temp['forecast'][$i]['predict_time'], 0, 10);
                $real[]     = $temp['real'][$i]['aqi'];
                $forecast[] = $temp['forecast'][$i]['aqi'];
            }
            
            // 格式化
            $list['days']      = implode(',', $days);
            $list['practical'] = implode(',', $real);
            $list['forecast']  = implode(',', $forecast);
        } catch (\Exception $e) {
            throw $e;
        }
        
        return $list;
    }
    
    /**
     * 获取7天的首要污染物
     * @author JiYun On 2019/03/04
     * @param array $param
     * @return array
     * @throws \Exception
     */
    public function primaryPollution($param = [])
    {
        // 设置默认值
        $param = $this->checkParam($param);
        
        $list = [];
        
        try {
            // 取数据
            $temp = $this->get7DayDatas($param);
            
            $days     = [];
            $real     = [];
            $forecast = [];
            
            // 合并列表
            for ($i = 0; $i < count($temp['forecast']); ++$i) {
                $days[] = mb_substr($temp['forecast'][$i]['predict_time'], 0, 10);
                
                $real[] = [
                    'name'  => $temp['real'][$i]['primary_pollutant_name'],
                    'value' => $temp['real'][$i]['primary_pollutant_value']
                ];
                
                $forecast[] = [
                    'name'  => $temp['forecast'][$i]['primary_pollutant_name'],
                    'value' => $temp['forecast'][$i]['primary_pollutant_value']
                ];
            }
            
            // 格式化
            $list['days']     = implode(',', $days);
            $list['real']     = $real;
            $list['forecast'] = $forecast;
        } catch (\Exception $e) {
            throw $e;
        }
        
        return $list;
    }
}
